using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Extensions;
using ShopPowerTemplate.Presentation.Mvc.Models;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services;

namespace ShopPowerTemplate.Presentation.Mvc.Controllers
{
	public partial class ProductController : ProductControllerBase
	{
        //Custom code here

        #region Product Detail

        [AcceptVerbs(HttpVerbs.Get)]
        public virtual ActionResult CreateProductDetail(int masterId = 0)
        {
            ProductDetail productDetail = new ProductDetail { Product = _productService.Find(masterId) };
            CreateProductDetailViewModel viewModel = Mapper.Map<ProductDetail, CreateProductDetailViewModel>(productDetail);

            return View("~/Views/ProductDetail/Create.cshtml", viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult CreateProductDetail(CreateProductDetailViewModel productDetailModel)
        {
            if (ModelState.IsValid)
            {
                ProductDetail productDetails = new ProductDetail();
                AutoMapper.Mapper.Map<CreateProductDetailViewModel, ProductDetail>(productDetailModel, productDetails);

                Product product = _productService.Find(productDetails.Product.Id);
                _productService.AddProductDetails(product, productDetails);

                return RedirectToAction("ChangeProductDetail", new { id = productDetails.Id });
            }
            else
            {
                return View("~/Views/ProductDetail/Create.cshtml", productDetailModel);
            }
        }

        public virtual ActionResult ChangeProductDetail(int id)
        {
            ProductDetail productDetail = _productDetailService.Find(id);
            EditProductDetailViewModel viewModel = Mapper.Map<ProductDetail, EditProductDetailViewModel>(productDetail);

            return View("~/Views/ProductDetail/Edit.cshtml", viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult ChangeProductDetail(EditProductDetailViewModel productDetailModel)
        {
            if (ModelState.IsValid)
            {
                ProductDetail productDetail = _productDetailService.Find(productDetailModel.Id);
                AutoMapper.Mapper.Map<EditProductDetailViewModel, ProductDetail>(productDetailModel, productDetail);

                _productDetailService.Change(productDetail);

                return RedirectToAction("Edit", new { id = productDetail.Product.Id });
            }
            else
            {
                return View("~/Views/ProductDetail/Edit.cshtml", productDetailModel);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public override ActionResult LoadProductDetails(string sidx, string sord, int page, int rows, string search, int mode = 1, int masterId = 0)
        {
            Product currentProduct = _productService.Find(masterId);
            List<ProductDetail> list = currentProduct.ProductDetails.ToList();
            int count = list.Count();

            var data = new
            {
                total = 1,        // Total pages
                page = 1,      // Current page
                records = count,  // Total records
                rows = from productDetail in list     // Row data
                       select new
                       {
                           id = productDetail.Id,
                           cell = new string[] {
                                productDetail.IdentityDescription,
                                productDetail.Stock.ToString()
                           }
                       }
            };

            // Return elements
            return Json(data);
        }

        #endregion Product Detail

        #region Image

        [AcceptVerbs(HttpVerbs.Get)]
        public virtual ActionResult CreateImage(int masterId = 0)
        {
            Image image = new Image { Product = _productService.Find(masterId) };
            CreateImageViewModel viewModel = Mapper.Map<Image, CreateImageViewModel>(image);

            return View("~/Views/Image/Create.cshtml", viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult CreateImage(CreateImageViewModel imageModel)
        {
            if (Request.Files["Data"].ContentLength == 0)
                ModelState.AddModelError(string.Empty, string.Format(Resources.Shared.Required, "Data"));

            if (ModelState.IsValid)
            {
                Image image = new Image();
                AutoMapper.Mapper.Map<CreateImageViewModel, Image>(imageModel, image);

                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];

                    if (inputTagName == "Data" && file.ContentLength > 0)
                    {
                        image.Data = Helpers.ZipUtil.Zip(file);
                    }
                }

                Product product = _productService.Find(image.Product.Id);
                _productService.AddImages(product, image);

                return RedirectToAction("Edit", new { id = image.Product.Id });
            }
            else
            {
                return View("~/Views/Image/Create.cshtml", imageModel);
            }
        }

        public virtual ActionResult ChangeImage(int id)
        {
            Image image = _imageService.Find(id);
            EditImageViewModel viewModel = Mapper.Map<Image, EditImageViewModel>(image);

            return View("~/Views/Image/Edit.cshtml", viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult ChangeImage(EditImageViewModel imageModel)
        {
            if (ModelState.IsValid)
            {
                Image image = _imageService.Find(imageModel.Id);
                AutoMapper.Mapper.Map<EditImageViewModel, Image>(imageModel, image);

                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];

                    if (inputTagName == "Data" && file.ContentLength > 0)
                    {
                        image.Data = Helpers.ZipUtil.Zip(file);
                    }
                }

                _imageService.Change(image);

                return RedirectToAction("Edit", new { id = image.Product.Id });
            }
            else
            {
                return View("~/Views/Image/Edit.cshtml", imageModel);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public override ActionResult LoadImages(string sidx, string sord, int page, int rows, string search, int mode = 1, int masterId = 0)
        {
            Product currentProduct = _productService.Find(masterId);
            List<Image> list = currentProduct.Images.ToList();
            int count = list.Count();

            var data = new
            {
                total = 1,        // Total pages
                page = 1,      // Current page
                records = count,  // Total records
                rows = from image in list     // Row data
                       select new
                       {
                           id = image.Id,
                           cell = new string[] {
                                image.Id.ToString(),
                                image.Name,
                                image.Description,
                                image.SortOrder.ToString()
                           }
                       }
            };

            // Return elements
            return Json(data);
        }

        #endregion Image
    }
}

